﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    public class TableExporter
    {
        public TableExporter() { }

        public void ExportToExcel(DataTable table, string strFilePath)
        {
            if (null != table)
            {
                Excel.Application _excel = null;
                Excel.Workbooks _books = null;
                Excel.Workbook _book = null;
                Excel.Worksheet _sheet = null;
                try
                {
                    _excel = new Excel.Application();
                    _books = _excel.Workbooks;
                    _book = _books.Add(Missing.Value);
                    _sheet = _book.Worksheets.get_Item(1) as Excel.Worksheet;
                    _sheet.Name = table.TableName;

                    if (0 < table.Columns.Count && 0 < table.Rows.Count)
                    {
                        for (int i = 0; i < table.Columns.Count; i++) _sheet.Cells[1, i + 1] = table.Columns[i].Caption;
                        for (int _row = 0; _row < table.Rows.Count; _row++)
                        {
                            for (int _column = 0; _column < table.Columns.Count; _column++)
                            {
                                object _cell = table.Rows[_row][_column];
                                if (null != _cell) _sheet.Cells[2 + _row, _column + 1] = _cell.ToString();
                            }
                        }
                    }
                    else throw new ArgumentException("빈 내용을 파일로 출력할 수 없습니다.");
                    SaveExcelFile(strFilePath, _book);
                    _book.Close(false, Missing.Value, Missing.Value);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (null != _sheet) System.Runtime.InteropServices.Marshal.ReleaseComObject(_sheet);
                    if (null != _book) System.Runtime.InteropServices.Marshal.ReleaseComObject(_book);
                    if (null != _books) System.Runtime.InteropServices.Marshal.ReleaseComObject(_books);
                    if (null != _excel) System.Runtime.InteropServices.Marshal.ReleaseComObject(_excel);
                }
            }
        }

        private void SaveExcelFile(string strPath, Excel.Workbook workbook)
        {
            string _path = Path.GetDirectoryName(strPath) + @"\" + Path.GetFileNameWithoutExtension(strPath);
            string _extension = Path.GetExtension(strPath);
            Excel.XlFileFormat _format = Excel.XlFileFormat.xlWorkbookDefault;
            if (!string.IsNullOrEmpty(_extension))
            {
                switch (_extension.ToLower())
                {
                    case ".xls":
                        _format = Excel.XlFileFormat.xlWorkbookNormal;
                        break;
                    case ".xlsx":
                        _format = Excel.XlFileFormat.xlOpenXMLWorkbook;
                        break;
                }
            }
            workbook.SaveAs(_path, _format, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
        }
    }
}